package business.knBase

import groovy.sql.GroovyRowResult
import groovy.sql.Sql

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import business.coding.CandidateCenter;

import domain.Center;

public class KnBaseGeneric implements Comparable<KnBaseGeneric> {
	private List<String> registry;
	
	public KnBaseGeneric() {
		 registry = new ArrayList<String>();
	}
	
	public List<String> getRegistry() {
		return registry;
	}
	
	public void add(String value) {
		registry.add(value)
	}
	
	public String get(int index) {
		return registry.get(index);
	}

	@Override
	public String toString() {
		return "KnBaseGeneric [registry=" + registry + "]";
	}

	/**
	 * Si todas las columnas estan en blanco es para borrar
	 * @return 
	 */
	public boolean empty() {
		boolean emp = true;
		
		for (String reg : registry) {
			if (!reg.trim().isEmpty())
				emp = false;
		}
		
		return emp;
		
	}
	
	/**
	* Meto este metodo para poder insertar Center en TreeSet de ComunFinder
	*/
   @Override
   public int compareTo(KnBaseGeneric o) {
	  	try {
			  if (equalregistry(this, o)) return 0;
			  else  if (o.registry.get(0).toLowerCase().charAt(0) > this.registry.get(0).toLowerCase().charAt(0)) return -1;
			  else return 1;
		
		} catch (Exception e) {
			//si salta alguna excepcion porque algun valor es null y no se puede comprar, se devuelve que son iguales
			return 1;
		}
   }
   
   @Override public boolean equals( Object aThat ) {
    Boolean result = equalregistry(this, aThat);
	return result;
  }

  @Override public int hashCode() {
    
	return registry.hashCode();
	
  }
   
   private boolean equalregistry(KnBaseGeneric reg1, KnBaseGeneric reg2) {
	   boolean allequals = true;
	   allequals = (reg1.registry.size() == reg2.registry.size());
	   
	   if (allequals) {
		   int i = 0;
		   while (allequals && i < reg1.registry.size()) {
			   allequals = reg1.registry.get(i).equalsIgnoreCase(reg2.registry.get(i));
			   i++;
		   }
	   }
	   
	   return allequals;
   }
	
}

/**
 * DAO para cualquier tabla de KnBase
 * @author Administrador
 *
 */
public class KnBaseGenericDAO {
	
	private def Sql sqlite;
	private String _table;
	
	/**
	 * Inicializamos la clase con la instance de SQL
	 * @param sql
	 */
	public KnBaseGenericDAO(Sql sql, String table) {
		sqlite = sql;
		_table = table;
	}
	
	

	
	
	/**
	 * Inserta un corp
	 * @param idcorp
	 * @param corp
	 */
	public void insert(KnBaseGeneric registry) {
		def values = []
		String marks = "";
		registry.getRegistry().each { val ->
			values.add(val); 
			marks += "?,"
		}
		marks = marks.substring (0, marks.length()-1);
		String sql = "insert into ${_table} values (${marks})"
		sqlite.execute(sql, values);
	}
	
	public void insertAll(List<KnBaseGeneric> l_regs) {
		l_regs.each { knreg ->
			if (!knreg.empty())
				insert(knreg);
		}
	}
	
	private int countColumns() {
		String sql = "select * from ${_table}"
		GroovyRowResult rowRs = sqlite.firstRow(sql);
		return rowRs.keySet().size();
	}
	
	public List<KnBaseGeneric> selectAll() {
		int cntCols = countColumns();
		List<KnBaseGeneric> l_regs = new ArrayList<KnBaseGeneric>();
		String sql = "select * from ${_table}"
		sqlite.eachRow(sql,{
			KnBaseGeneric reg = new KnBaseGeneric();
			for (int i = 0; i < cntCols; i++) {
				reg.add(it[i]);
			}
			l_regs.add(reg);
		});
	
		Collections.sort(l_regs);
		return l_regs;
	}
	
	public void deleteAll() {
		String sql = "delete from ${_table}"
	   sqlite.execute(sql);
	}
		
	
}
